package com.aorise.controller.system;

import com.aorise.model.system.*;
import com.aorise.service.system.SysConfigService;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:cat
 * @Desicription: 系统配置控制器类
 * @Date:Created in 2019-05-28
 * @Modified By:
 */
@RestController
@Api(description ="系统配置信息接口")
public class SysConfigController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 根据类别查询系统配置信息
     * HTTP 方式：GET
     * API 路径：/api/sysConfig/conType/{conType}
     * 方法名：GetSysConfig
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-28
     */
    @ApiOperation(value = "根据类别查询系统配置信息", httpMethod = "GET", response = String.class,notes = "根据类别查询系统配置信息")
    @RequestMapping(value = "/api/sysConfig/conType/{conType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSysConfig(@ApiParam(value = "类别", required = true) @PathVariable(value = "conType", required = true) Integer conType) {
        logger.debug("Request RESTful API:GetSysConfig");
        logger.debug("conType" + conType);

        try {
            List<SysConfigModel> sysConfigModels = sysConfigService.getSysConfig(conType);

            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "",sysConfigModels).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:SysConfigController. function:getSysConfig...msg:Get SysConfig DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:SysConfigController. function:getSysConfig...msg:Get SysConfig Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 修改系统配置信息
     * HTTP 方式：PUT
     * API 路径：/api/sysConfig
     * 方法名：updateSysConfig
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-05-28
     */
    @ApiOperation(value = "修改系统配置信息", httpMethod = "PUT", response = String.class, notes = "通过主键、配置项、类别修改系统配置信息")
    @RequestMapping(value = "/api/sysConfig", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    String updateSysConfig(@RequestBody @Validated List<SysConfigEditVO> sysConfigEditVOList, BindingResult result) {
        logger.debug("Request RESTful API:updateSysConfig");
        logger.debug("sysConfigEditVO：" + sysConfigEditVOList);

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            int ret = sysConfigService.updateSysConfig(sysConfigEditVOList);
            if (ret > 0) {
                return new JsonResponseData(true,
                        StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "","").toString();
            } else {
                return new JsonResponseData(false,
                        "", StatusDefine.FAILURE, StatusDefineMessage.GetMessage(StatusDefine.FAILURE),"").toString();
            }
        } catch (DataAccessException e) {
            logger.error(
                    "controller:SysConfigController. function:updateSysConfig...msg:Update SysConfig DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:SysConfigController. function:updateSysConfig...msg:Update SysConfig Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

}
